package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.cache.ShopCache;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.mapper.BaseCategoryViewMapper;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author GuoJH
 * @since 2021-08-27
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {
    @ShopCache(prefix = "product:indexCategoryInfo:")
    //获取商品首页的分类信息
    @Override
    public List<JSONObject> getIndexCategoryInfo() {
        //先获取所有的分类信息
        List<BaseCategoryView> baseCategoryViews = baseMapper.selectList(null);
        //在通过一级分类进行分组
        Map<Long, List<BaseCategoryView>> category1Map =
                baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //再将获取的一级分类map进行迭代
        Set<Map.Entry<Long, List<BaseCategoryView>>> category1Entries = category1Map.entrySet();
        //定义一个index,返回给前端
        int index = 1;
        //最终返回的是json数据
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : category1Entries) {
            //获取一级分类的id 和 所有的值
            Long category1Id = category1Entry.getKey();
            List<BaseCategoryView> category1ValueList = category1Entry.getValue();

            //根据前端需要的值  进行返回
            JSONObject category1Json = new JSONObject();
            String category1Name = category1ValueList.get(0).getCategory1Name();
            category1Json.put("index", index++);
            category1Json.put("categoryId", category1Id);
            category1Json.put("categoryName", category1Name);


            //在获取二级分类的id 和对应值
            Map<Long, List<BaseCategoryView>> category2Map =
                    category1ValueList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Set<Map.Entry<Long, List<BaseCategoryView>>> category2MapEntries = category2Map.entrySet();
            List<JSONObject> jsonCategory1Children = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2MapEntry : category2MapEntries) {
                //获取二级分类的id和名称
                Long category2Id = category2MapEntry.getKey();
                List<BaseCategoryView> category2ValueList = category2MapEntry.getValue();

                JSONObject category2Json = new JSONObject();
                category2Json.put("categoryId",category2Id);
                category2Json.put("categoryName",category2ValueList.get(0).getCategory2Name());
                jsonCategory1Children.add(category2Json);


                //获取三级分类的id和名称
                List<JSONObject> jsonCategory2Children = new ArrayList<>();
                category2ValueList.stream().forEach((baseCategoryView)->{
                    Long category3Id = baseCategoryView.getCategory3Id();
                    String category3Name = baseCategoryView.getCategory3Name();

                    JSONObject category3Json = new JSONObject();
                    category3Json.put("categoryId",category3Id);
                    category3Json.put("categoryName",category3Name);
                    jsonCategory2Children.add(category3Json);
                });

                //二级分类下的categoryChild
                category2Json.put("categoryChild",jsonCategory2Children);
            }
            //一级分类下的categoryChild
            category1Json.put("categoryChild",jsonCategory1Children);

            //需要返回的数据
            jsonObjectList.add(category1Json);
        }

        return jsonObjectList;
    }
}
